package com.yanqu.road.dao.impl.activity.carptrip;

import com.alibaba.fastjson.JSON;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.carptrip.data.CarpTripPvPReportData;
import com.yanqu.road.entity.player.UserBaseInfo;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CarpTripPvPReportDataDaoImpl extends TempDataObjectDao<CarpTripPvPReportData> {

    public static String getCarpTripPvPReportDataSql = "select * from `t_u_carp_trip_pvp_report` where `activityId`=? and `userId`=? order by `time` desc";
    public List<CarpTripPvPReportData> getCarpTripPvPReportData(int activityId, long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        List<CarpTripPvPReportData> list = new ArrayList<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getCarpTripPvPReportDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setLong(i++, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                CarpTripPvPReportData data = parse(rs);
                list.add(data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return list;
    }

    public static String insertCarpTripPvPReportDataSql = "insert into `t_u_carp_trip_pvp_report`(activityId,cellId,userId,ability,enemyUserId,enemyUserBaseInfo,enemyAbility,oldCellNum,newCellNum,time) " +
            "values (?,?,?,?,?,?,?,?,?,?)";

    @Override
    protected boolean addData(CarpTripPvPReportData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getCellId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getAbility()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getEnemyUserId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getEnemyUserBaseInfo())));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getEnemyAbility()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getOldCellNum()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getNewCellNum()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getTime()));
        if (getDb().executeNoneQuery(insertCarpTripPvPReportDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected boolean updateData(CarpTripPvPReportData data) {
        return false;
    }

    @Override
    protected CarpTripPvPReportData parseData(ResultSet rs) throws SQLException {
        CarpTripPvPReportData data = new CarpTripPvPReportData();
        data.setActivityId(rs.getInt("activityId"));
        data.setCellId(rs.getInt("cellId"));
        data.setUserId(rs.getLong("userId"));
        data.setAbility(rs.getString("ability"));
        data.setEnemyUserId(rs.getLong("enemyUserId"));
        data.setEnemyUserBaseInfo(JSON.parseObject(rs.getString("enemyUserBaseInfo"), UserBaseInfo.class));
        data.setEnemyAbility(rs.getString("enemyAbility"));
        data.setOldCellNum(rs.getInt("oldCellNum"));
        data.setNewCellNum(rs.getInt("newCellNum"));
        data.setTime(rs.getLong("time"));

        return data;
    }
}
